<?php
namespace Apps\BoxSns\Model;

use Think\Model;

class SnsGroupModel extends \BaseModel {
	protected $model_group_user;
	protected $model_group_statistics;

	public function _initialize() {
		parent::_initialize();
		$this->model_group_user = BoxModel("sns_group_user");
	}

	/*******群组信息******/
	public function load($id) {
		$info = $this->find($id);
		return $info;
	}

	/*******创建群组*****/
	public function create_group($data) {
		if ($group_id = $data['id']) {
			$this->where("id='$group_id'")->save($data);
		} else {
			$data['addtime'] = time();
			$group_id = $this->add($data);
			if ($data['user_id']) {
				$this->join_group($data['user_id'], $group_id, $level = 1);
			}
		}
		return $group_id;
	}

	/*******加入群组****/
	public function join_group($user_id, $group_id, $level = 2) {
		$row = array(
			'user_id' => $user_id,
			'join_time' => time(),
			'group_id' => $group_id,
			'level' => $level,
			'lastupdate' => time(),
		);
		$id = $this->model_group_user->add($row);
		if ($id) {
			$this->where(array('id' => $group_id))->setInc('user_count');
		}
		return $id;
	}

	/*******退出群组****/
	public function quit_group($user_id, $group_id) {
		//dump();
		$id = $this->model_group_user->where(array('group_id' => $group_id, 'user_id' => $user_id))->delete();
		if ($id) {
			$this->where(array('id' => $group_id))->setDec('user_count');
		}
		return $id;
	}

	/*****禁示发言****/
	public function forbid_group($user_id, $group_id) {
		$id = $this->model_group_user->where(array('id' => $group_id, 'user_id' => $user_id))->save(array('level' => 3));
		return $id;
	}

	/******删除一个群组***/
	function del_group($group_id) {
		$r = $this->where(array('id' => $group_id))->save(array('is_show' => 0));
		return $r;
	}

	/******帖子数加1***/
	public function statistics($id, $type = "setInc") {
		$this->where(array('id' => $id))->$type("topic_count");
		$this->where(array('id' => $id))->save(array('updatetime' => time()));

	}

	/******用户与群组关系******/
	public function getRel($user_id, $group_id) {
		$r = BoxModel("sns_group_user")->where("group_id='$group_id' and user_id='$user_id'")->find();
		if ($r['level'] == 1) {
			return "master";
		}
		if ($r['level'] == 3) {
			return "black";
		}
		if ($r['level'] == 2) {
			return "join";
		}
		$r = BoxModel("sns_group_invite")->where("group_id='$group_id' and user_id='$user_id'")->find();
		if ($r) {
			return "apply";
		}
	}

	/******群组申请表列******/
	public function group_apply($user_id, $limit = 50) {
		$model = BoxModel("sns_group_invite");
		$map = array(
			'limit' => $limit,
			'where' => "user_id='$user_id'",
		);
		$data = $model->getList($map);
		$model = BoxModel("SystemMember");
		$model2 = BoxModel("SnsGroup");
		if (is_array($data['list'])) {
			foreach ($data['list'] as $key => $one) {
				$row = array(
					'user_info' => $model->load($one['to_user_id']),
					'group_info' => $model2->load($one['group_id']),
				);
				$data['list'][$key] = $row;
			}
		}
		return $data;
	}

	/***********群组的成员***/
	public function user($group_id, $limit = 50) {
		$model = BoxModel("sns_group_user");
		$map = array(
			'limit' => $limit,
			'where' => "group_id='$group_id'",
		);
		$data = $model->getList($map);
		$model = BoxModel("SystemMember");
		if (is_array($data['list'])) {
			foreach ($data['list'] as $key => $one) {
				$data['list'][$key] = $model->load($one['user_id']);
			}
		}
		return $data;
	}

}
